package zdream.pmfield.stage.object;

import java.util.Objects;
import java.util.function.Supplier;

import zdream.pmfield.stage.Stage;
import zdream.pmfield.stage.action.StageAction;

/**
 * @author Zdream
 * @date 2023-11-21
 * @since 0.0.1
 */
public abstract class ObjectOnSeat implements IStageTarget {
	public final transient Stage stage;

	protected ObjectOnSeat(Stage stage) {
		this.stage = Objects.requireNonNull(stage);
	}

	@Override
	public Stage getStage() {
		return stage;
	}

	public boolean isFoe() {
		return false;
	}

	public FoeActor asFoe() {
		throw new IllegalArgumentException(
				getClass().getSimpleName() + " is not a " + FoeActor.class.getSimpleName());
	}

	/**
	 * 允许同时占多个 {@link Seat}, 但是这里只记录一个.
	 * 其它的作为 {@link Shadow} 存在
	 *
	 * 不在场时 seat = null
	 */
	public Seat seat;

	public void arrive(Seat seat) {
		if (this.seat == Objects.requireNonNull(seat)) {
			return;
		}

		if (this.seat != null) {
			leave();
		}
		this.seat = seat;
		seat.objectArrive(this);
	}

	public void leave() {
		if (this.seat == null) {
			return;
		}
		final Seat seat = this.seat;
		this.seat = null;
		seat.objectLeave(this);
	}

	public void leaveFromStage() {
		leave();
	}

	public Supplier<Boolean> putActionNow(StageAction action) {
		return stage.timeline.putActionNow(action);
	}

	public Supplier<Boolean> putActionWithTime(StageAction action, float time) {
		return stage.timeline.putActionWithTime(action, time);
	}
}
